﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    public class ItemService
    {
        MarketService marketService = new MarketService();
        UserService userService = new UserService();
        public ItemService() { }

        public void AddItem(Item item)
        {
            using (var ctx = new SystemContext())
            {
                if (item == null) { return; }
                ctx.Items.Add(item);
                ctx.SaveChanges();
            }
        }
        public void DeleteItem(Item item)
        {
            using (var ctx = new SystemContext())
            {
                var item1 = ctx.Items
                    .SingleOrDefault(s => s.ItemID == item.ItemID);

                if (item1 == null) { return; }
                ctx.Items.Remove(item1);
                ctx.SaveChanges();
            }
        }
        public void ModifyItem(Item olditem,Item newitem)
        {
            DeleteItem(olditem);
            AddItem(newitem);
        }
        public List<Item> GetAllItem()
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Items
                    .ToList();
            }
        }
        public List<Item> GetItemByItemID(string itemid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Items
                    .Where(s => s.ItemID == itemid)
                    .ToList();
            }
        }
        public List<Item> GetItemByMarketID(string marketid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Items
                    .Where(s => s.MarketID == marketid)
                    .ToList();
            }
        }
        public List<Item> GetItemByKey(string key)//根据商品名关键字找商品
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Items
                    .Where(s => s.ItemName.Contains(key))
                    .ToList();
            }
        }
        public List<Market> GetMarketByItem(Item item)//根据商品找其所属的超市
        {
            return marketService.GetMarketByMarketID(item.MarketID);
        }
        public List<User> GetUserByItem(Item item)//根据商品找其所属超市的所属用户
        {
            List<Market> markets = marketService.GetMarketByMarketID(item.MarketID);
            return userService.GetUserByID(markets[0].UserID);
        }
        public List<Item> GetItemExceptUser(User user)//返回商品，除了该用户超市的商品
        {
            List<Market> userMarket = new List<Market>();
            userMarket = marketService.GetMarketByUserID(user.UserID);
            if (userMarket.Count == 0)//如果该用户没有超市，则返回所有商品
            {
                using (var ctx = new SystemContext())
                {
                    return ctx.Items
                        .ToList();
                }
            }
            else//如果该用户有超市，则返回除了该用户超市的商品
            {
                using (var ctx = new SystemContext())
                {
                    var allItems = ctx.Items.ToList();
                    return allItems
                        .Where(s => s.MarketID != userMarket[0].MarketID)
                        .ToList();
                }
            }
        }
        public List<Item> GetItemByMarketIDandKey(string marketid, string key)// 根据超市id和关键字搜索
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Items
                    .Where(s => s.MarketID == marketid && s.ItemName.Contains(key))
                    .ToList();
            }
        }
    }
}
